package com.arashivision.airlive;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.arashivision.insta360.export.exporter.ARComposerConfig;
import com.arashivision.insta360.export.services.ExportManager;
import com.arashivision.insta360.export.services.OnExportListener;
import com.arashivision.insta360.export.services.Request;
import com.arashivision.insta360.sdk.render.renderer.model.sticker.StickerFactory;
import com.arashivision.insta360.sdk.render.renderer.model.sticker.StickerInfo;

import java.io.File;
import java.net.URLDecoder;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ExportImageActivity1 extends AppCompatActivity {

    private static final String TAG = "ExportImageActivity1";
    @Bind(R.id.id_tv_log)
    TextView mTvlog;

    @Bind(R.id.id_iv_image)
    ImageView mIvImage;

    private ExportManager mExportManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_export_image1);
        ButterKnife.bind(this);
        mExportManager = new ExportManager(this.getApplicationContext());
    }


    @Override
    protected void onResume() {
        super.onResume();
        mExportManager.register();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mExportManager.unregister();
    }

    public void onClickExport(View view){
//        final String url = "/storage/emulated/0/Insta360Player/IMG_20170301_143409_833.insp";
        final String url = "/storage/emulated/0/Insta360Air/galleryOriginal/VID_20170713_185002_289.insv";
        if(!TextUtils.isEmpty(url)) {

//            Log.i("4444", "te:" + ExtraDataOperator.getInstace().getData(url).getStringId());

            setText("视频地址：" + url + "\n");
            String tempPath = "/mnt/sdcard/" + getFileName(url) + ".jpg";
            try {
                tempPath = URLDecoder.decode(tempPath, "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
            }
            final String outputPath = tempPath;
            setText("输出地址：" + outputPath + "\n");
            Request request = new Request(ARComposerConfig.TYPE_THUMBNAIL_STITCH);
            request.setInput(url);
            request.setOutput(outputPath);
            request.setHeight(0);
            request.setColor(0xff000000);

            StickerInfo stickerInfo = StickerFactory.createStickerInfo(R.mipmap.ic_launcher, 50, 90, false);
            request.setSticker(stickerInfo);
//            ExtraDataOperator.Data extraData = ExtraDataOperator.getInstace().getData(request.getInput());

//            if (extraData != null) {
//                CImageGyroController cImageGyroController = new CImageGyroController(extraData.getInfo().getGyro().getGyro());
//                //陀螺仪校准
//                Matrix4 postMatrix = cImageGyroController.getGyroQuaternion().toRotationMatrix();
//                request.setPostMatrix(postMatrix);
//                //手动校准
////                ExtraDataOperator.Data.ExtraData.Euler euler = extraData.getExtraData().getEuler();
//                Matrix4 gestureMatrix = GestureController.getMatrixFromEuler(euler);
//                request.setPreMatrix(gestureMatrix);
//            }
            mExportManager.setOnExportListener(new OnExportListener() {
                @Override
                public void onComplete(String id) {
//                    ExtraDataOperator.Data date = ExtraDataOperator.getInstace().getData(outputPath);
//
//                    date.getInfo().setCreationTime(System.currentTimeMillis());
//                    ExtraDataOperator.Data.Info.Gps gps = new ExtraDataOperator.Data.Info.Gps();
//                    gps.setAltitude(0.0);
//                    gps.setLatitude(0.0);
//                    gps.setLongitude(0.0);
//                    date.getInfo().setGps(gps);
//                    try {
//                        ExtraDataOperator.getInstace().save(outputPath);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mIvImage.getLayoutParams();
                    layoutParams.height = (int) (mIvImage.getMeasuredWidth() / 2.0F);
                    layoutParams.width = mIvImage.getMeasuredWidth();
                    mIvImage.setLayoutParams(layoutParams);
                    mIvImage.setImageBitmap(BitmapFactory.decodeFile(outputPath));
                }

                @Override
                public void onCancel(String id) {
                }

                @Override
                public void onError(String id, int code) {
                }

                @Override
                public void onProgress(String id, int progress) {

                }

                @Override
                public void onFileSizeChanged(String s, String s1, long l) {

                }
            });
            mExportManager.enqueue(request);
        }
    }




    private void setText(final String str){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mTvlog.setText(mTvlog.getText() + str);
            }
        });
    }

    private String getFileName(String url){
        url = url.toLowerCase();
        if(url.startsWith("/")){
            String name = new File(url).getName();
            return name.substring(0, name.indexOf("."));
        }else if(url.startsWith("http")){
            String name = url.substring(url.lastIndexOf("/") + 1, url.length());
            return name.substring(0, name.indexOf("."));
        }
        return "";
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
